function [QValuesSave, AbsorptionSave] = Spara2SandFom(runIDs, ParameterRange, FrequencySelectRange, PolarizedDirection, FileTag, SaveFlag, TwoPeak)
    % 从当前打开的CST窗口，根据S参数计算吸收谱，灵敏度，FOM，保存在CST所在文件位置
    %
    % @since 2022.3.16 增加峰值高度
    % @param  [runIDs] runID向量.
    % @param  [ParameterRange] 对应的参数值变化范围.
    % @param  [FrequencySelectRange] 需要计算Q值的范围.
    % @param  [PolarizedDirection] 极化方向.
    % @param  [FileTag] 保存文件名中加入的标志.
    % @param  [SaveFlag] 是否保存文件，保存则不为0.
    % @param  [TwoPeak] 所选频率范围有两个峰是，计算第一个(-1)还是第二个(其他值).
    % @return  [QValuesSave] Q值，ID，和参数值.
    % @return  [AbsorptionSave] 频率与各个参数对应的吸收值.
    %
    % 使用案例1：
    %    无参数调用， [~,~] = Spara2QandA()
    %    将按runID返回所有值，Q值默认计算第一个峰，默认频率范围请查看
    %    极化方向为Y，不保存文件
    % 使用案例2：
    %    [~,~]=Spara2QandA(1:31,-85:5:60,[1.1,1.3],'Y','Full_Dx',0,-1)
    %

    %     close all;
    CST_Post = CST_MicrowaveStudio();

    if nargin == 0
        runIDs = CST_Post.getRunIDs;
        FrequencySelectRange = [1.18, 1.28];
        PolarizedDirection = 'Y';
        FileTag = [];
        SaveFlag = 0;
        TwoPeak = -1;

    end

    QValuesSave = [];
    AbsorptionSave = [];
    LocationSave = [];

    for everyrun = runIDs
        % fprintf('\n------本次处理的是runID:%i------------ \n', everyrun);

        [freq, sparam, ~] = CST_Post.getSParams(everyrun);

        if PolarizedDirection == 'Y'
            [Output_Q, Output_Abp, peakLocation, peakValue] = obtainAbsorptionQvalueLocation(sparam, freq, FrequencySelectRange);

        elseif PolarizedDirection == 'X'
            [Output_Q, Output_Abp, peakLocation, peakValue] = obtainAbsorptionQvalueLocation(sparam(:, 3:4), freq, FrequencySelectRange);
        end

        if TwoPeak == -1
            Output_Q = Output_Q(1);
            peakValue = peakValue(1);
            peakLocation = peakLocation(1);
            QValuesSave = [QValuesSave; Output_Q, everyrun, peakValue, Output_Q * peakValue];
            LocationSave = [LocationSave; peakLocation, everyrun];
        else
            Output_Q = Output_Q(end);
            peakValue = peakValue(end);
            peakLocation = peakLocation(end);
            QValuesSave = [QValuesSave; Output_Q(end), everyrun, peakValue, Output_Q * peakValue];
            LocationSave = [LocationSave; peakLocation, everyrun];
        end

        AbsorptionSave = [AbsorptionSave, Output_Abp(1:1001)];
        fprintf(' runID:%d ----------- Q_Value:%f \n', everyrun, Output_Q);

    end

    % 计算灵敏度
    S = polyfit(ParameterRange', LocationSave(:, 1),1);
    fittedValue = polyval(S, ParameterRange');
    fprintf('灵敏度为%f--', S(1));
    [~]=YW_Plot_Fast({ParameterRange', LocationSave(:, 1), ParameterRange', fittedValue}, {'*r', 'g-'}, ["FL", "Fitted"], ["RI", "FrequencyLocation"]);
    title(["灵敏度为",S(1)]);
    hold on;

    % 计算FOM
    FOM = abs(S(1))*QValuesSave(:, 1)./LocationSave(:, 1);
    [~]=YW_Plot_Fast({ParameterRange', FOM},{'*R'},["FOM"],["RI", "FOM"]);
    
%     FS = LocationSave(:, 1) - LocationSave(1, 1);
%     fittedCoefficient = polyfit(ParameterRange', FS .* QValuesSave(:, 1) ./ LocationSave(:, 1),1);
%     fittedValue = polyval(fittedCoefficient, ParameterRange');
% 
%     
%     fittedCoefficient_1 = polyfit(ParameterRange', QValuesSave(:, 1),1);
%     fittedValue1 = polyval(fittedCoefficient_1, ParameterRange');
%     fprintf('FOM00为 %f-', fittedCoefficient(1));
%     fprintf('FOM11为 %f-', fittedCoefficient_1(1));
%     YW_Plot_Fast({ParameterRange', FS .* QValuesSave(:, 1) ./ LocationSave(:, 1), ParameterRange', fittedValue,...
%        ParameterRange',fittedValue1 }, {'*r', 'g-','b:'}, ["FOM", "Fitted","Fitted1"], ["RI", "FrequencyLocation"]);

    %-----------------
    xarray = 1:length(runIDs);
    xarray = ParameterRange';
    [~]=YW_Plot_Fast({xarray,QValuesSave(:, 1),xarray,QValuesSave(:, 3),xarray,QValuesSave(:, 4)},...
        {'b--','g:','r*-'},["Q","P","Q*P"],["X","Y"]);
    figure;contourf(AbsorptionSave);
    %-----------------
    if SaveFlag

        SaveFile([freq, AbsorptionSave], [CST_Post.folder, '\Fre_Absorption_', FileTag, ], 'txt');
        SaveFile([QValuesSave, ParameterRange'], [CST_Post.folder, '\Q_', FileTag], 'txt');
        disp(['文件保存成功！'])
    end

    function [Output_Q, Output_Abp, Output_x, Output_y] = obtainAbsorptionQvalueLocation(sparam, freq, FrequencySelectRange)
        Absorption = 1 - abs(sparam(:, 1)).^2 - abs(sparam(:, 2)).^2;

        IndicsRange = find(FrequencySelectRange(1) <= freq & FrequencySelectRange(2) >= freq);
        SeperatedPeak = Absorption(IndicsRange);
        SeperatedFreq = freq(IndicsRange);
        clearvars sparm freq IndicsRange

        try
            [y, x, width, ~] = findpeaks(SeperatedPeak, SeperatedFreq, 'widthreference', 'halfheight','MinPeakHeight',0.1);
            Q_Value = x ./ width;
            Q_Value(1); % 看一下是否为空
        catch ME

            if (strcmp(ME.identifier, 'MATLAB:badsubscript'))
                fprintf('*\n*\n*\n索引超出范围，具体请查看峰的形状（已经将Q值取为INf)*\n*\n*\n')
                Q_Value = Inf;
                y = NaN;
                x = NaN;

            end

        end

        Output_Abp = Absorption;
        Output_Q = Q_Value;
        Output_y = y;
        Output_x = x;

    end

end
